﻿// -----------------------------------------------------------------------
// <copyright file="TimeLine.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace Epam.PhotoGallery.Site.Models
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web;
	using Epam.PhotoGallery.Interfaces.DataTransferObjects;
	using Epam.PhotoGallery.Interfaces.Providers;

	/// <summary>
	/// Operating model class.
	/// </summary>
	public class TimeLine
	{
		#region Properties

		/// <summary>
		/// Gets or sets the default event.
		/// </summary>
		/// <value>
		/// The default event.
		/// </value>
		public TimeItem DefaultEvent { get; set; }

		/// <summary>
		/// Gets or sets the events.
		/// </summary>
		/// <value>
		/// The events.
		/// </value>
		public IEnumerable<TimeItem> Events { get; set; }

		#endregion

		/// <summary>
		/// Gets the model.
		/// </summary>
		/// <param name="provider">The provider.</param>
		/// <param name="repository">The repository.</param>
		/// <returns>
		/// Return timeline.
		/// </returns>
		public static TimeLine GetModel(ITimelineDataProvider provider, string repository)
		{
			HttpContext.Current.Application.Lock();
			var memoryModel = HttpContext.Current.Application["TimeLine"];
			HttpContext.Current.Application.UnLock();
			TimeLine result;
			if (memoryModel == null)
			{
				UpdateModel(provider, repository);
				result = GetModel(provider, repository);
			}
			else
			{
				result = memoryModel as TimeLine;
			}
			return result;
		}

		/// <summary>
		/// Updates the model.
		/// </summary>
		/// <param name="provider">The provider.</param>
		/// <param name="repository">The repository.</param>
		public static void UpdateModel(ITimelineDataProvider provider, string repository)
		{
			var result = new TimeLine();
			var items = provider.GetAvailableAlbums(repository);

			if (items != null)
			{
				result.Events = items;
				////result.DefaultEvent = provider.GetCover(repository);
				////result.DefaultEvent.StartDate = result.Events.Min(t => t.StartDate);
			}
			else
			{
				////result.DefaultEvent = new TimeItem();
				result.Events = Enumerable.Empty<TimeItem>();
			}
			result = Sort(result);
			HttpContext.Current.Application.Lock();
			HttpContext.Current.Application["TimeLine"] = result;
			HttpContext.Current.Application.UnLock();
		}

		private static TimeLine Sort(TimeLine result)
		{
			result.Events = result.Events.OrderBy(t => t.StartDate);
			return result;
		}
		/// <summary>
		/// Gets the album cover path by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns>
		/// Return cover image source string.
		/// </returns>
		public string GetAlbumCoverPathById(Guid id)
		{
			return this.Events.First(t => t.Id.Equals(id)).CoverImageSource;
		}

		/// <summary>
		/// Gets the album path by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns>
		/// Return album path string.
		/// </returns>
		public string GetAlbumPathById(Guid id)
		{
			return this.Events.First(t => t.Id.Equals(id)).AlbumPath;
		}

		/// <summary>
		/// Gets the image path by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns>
		/// Return image path string.
		/// </returns>
		public string GetImagePathById(Guid id)
		{
			var result = string.Empty;

			foreach (var e in this.Events)
			{
				if (e.Images != null)
				{
					var image = e.Images.FirstOrDefault(t => t.Id.Equals(id));
					if (image != null)
					{
						result = image.Path;
						break;
					}
				}
			}

			return result;
		}
	}
}
